package com.martin.lib_base.impl

import com.martin.lib_base.LibBase
import com.martin.lib_base.ktx.logw
import okhttp3.logging.HttpLoggingInterceptor
import org.json.JSONArray
import org.json.JSONObject

class HttpLogImpl : HttpLoggingInterceptor.Logger {

    override fun log(message: String) {
        //非Debug情况下，不打印日志
        if (LibBase.debug) {
            try {
                //如果是Json数据，则以Json格式打印，便于查看
                when {
                    message.startsWith("{") -> logw(JSONObject(message).toString(2))
                    message.startsWith("[") -> logw(JSONArray(message).toString(2))
                    else -> logw(message)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                logw(message)
            }
        }
    }

}